package com.fanshuai.soring.boot.mvc.metrics;

import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.common.TextFormat;
import lombok.extern.slf4j.Slf4j;

import java.io.StringWriter;
import java.util.Enumeration;

@Slf4j
public class MetricsOutput {

    public String metrics() {
        StringWriter writer = new StringWriter();
        Enumeration<Collector.MetricFamilySamples> samplesEnumeration = CollectorRegistry.defaultRegistry.metricFamilySamples();

        try {
            TextFormat.write004(writer, samplesEnumeration);
        } catch (Exception e) {
            log.error(e + "");
        }
        return writer.toString();
    }
}
